package com.bracketbird.client.gui.club.tournament.rtc;


import com.bracketbird.client.model.tournament.*;
import com.bracketbird.clientcore.gui.*;
import com.bracketbird.clientcore.style.*;

import java.util.*;

/**
 *
 */
public class AllRankingPanel extends VerticalComponent {


    private List<LevelRankingPanel> levelRankPanels = new ArrayList<LevelRankingPanel>();

    public AllRankingPanel() {
        super();
        init();
    }

    private void init() {
        List<TournamentLevel> list = RTC.getInstance().getTournament().getLevels();
        if (levelRankPanels.size() < list.size()) {
            for (TournamentLevel l : list) {
                createPanel(l);
            }
        }
        RTC.getInstance().getTournament().addLevelListener(new TournamentListener<TournamentLevelEvent>() {
            public void onChange(TournamentLevelEvent event) {
                if (event.getAction() == TournamentLevelEvent.LevelAction.create) {
                    createPanel(event.getLevel());
                }
                else if (event.getAction() == TournamentLevelEvent.LevelAction.delete) {
                    deletePanel();
                }
            }
        });


    }

    private void createPanel(TournamentLevel level) {
        if(level.isCup()){
            return;
        }
        LevelRankingPanel lp = new LevelRankingPanel(level, levelRankPanels.size() + 1);
        add(lp, new TextLayout(null, "100%"));
        levelRankPanels.add(lp);
    }


    private void deletePanel() {
        if (!levelRankPanels.isEmpty()) {
            levelRankPanels.remove(levelRankPanels.size() - 1).removeFromParent();
        }
    }
}
